package org.apache.solr.handler.dataimport.scheduler;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.apache.solr.core.SolrResourceLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 配置文件读取类
 * @author IMZHANGJIE.CN
 *
 */
public class SolrDataImportProperties {

	private Properties properties;

	public static final String SERVER = "server";
	public static final String PORT = "port";
	public static final String WEBAPP = "webapp";
	
	public static final String ENABLED = "enabled";
	public static final String CORES = "cores";	
	public static final String ENTITY = "entity";
	public static final String PARAMS = "params";
	public static final String BEGINTIME = "beginTime";
	public static final String INTERVAL = "interval";
	public static final String QUERY = "query";

	public static final String DELTA_INDEX = "delta";//增量索引
	public static final String FULL_INDEX = "full";//重建索引
	public static final String REMOVE_INDEX = "remove";//删除索引
	public static final String OPTIMIZED_INDEX = "optimized";//优化索引
	
	private static final Logger logger = LoggerFactory.getLogger(SolrDataImportProperties.class);

	public SolrDataImportProperties() {
	}

	@SuppressWarnings("deprecation")
	public void loadProperties(boolean force) {
		SolrResourceLoader loader = null;
		try {
			loader = new SolrResourceLoader();
			logger.info("Instance dir = " + loader.getInstanceDir());

			String configDir = loader.getConfigDir();
			configDir = SolrResourceLoader.normalizeDir(configDir);
			if (force || properties == null) {
				properties = new Properties();

				String dataImportPropertiesPath = configDir + "dataimport.properties";

				FileInputStream fis = new FileInputStream(
						dataImportPropertiesPath);
				properties.load(fis);
			}
		} catch (FileNotFoundException fnfe) {
			logger.error( "Error locating DataImportScheduler dataimport.properties file", fnfe);
		} catch (IOException ioe) {
			logger.error("Error reading DataImportScheduler dataimport.properties file", ioe);
		} catch (Exception e) {
			logger.error("Error loading DataImportScheduler properties", e);
		} finally{
			if(loader != null){
				try {
					loader.close();
				}catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public String getProperty(String key) {
		return properties.getProperty(key);
	}
	
}
